package com.yzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yzg.mapper.FlashPromotionMapper;
import com.yzg.pojo.FlashPromotion;
import com.yzg.service.FlashPromotionService;
@Service
public class FlashPromotionServiceImpl implements FlashPromotionService{

    @Autowired
    private FlashPromotionMapper flashPromotionMapper;
    @Override
    public Map<String, Object> list(Integer pageNum, Integer pageSize, String keyword) {
        IPage<FlashPromotion> iPage=new Page<>(pageNum,pageSize);
        QueryWrapper<FlashPromotion> queryWrapper=new QueryWrapper<>();
        if (keyword!=null && !"".equals(keyword)){
            queryWrapper.like("title", keyword);
        }
        IPage<FlashPromotion> selectPage = flashPromotionMapper.selectPage(iPage, queryWrapper);
        List<FlashPromotion> records = selectPage.getRecords();
        long total = selectPage.getTotal();
        long totalPage = selectPage.getPages();
        Map<String, Object> map=new HashMap<>();
        map.put("list", records);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("totalPage",totalPage);
        map.put("total", total);
        return map;
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        FlashPromotion flashPromotion=new FlashPromotion();
        flashPromotion.setId(id).setStatus(status);
       return flashPromotionMapper.updateById(flashPromotion);
    }

    @Override
    public int savaCreate(FlashPromotion flashPromotion) {
        return flashPromotionMapper.insert(flashPromotion);
    }

    @Override
    public int updateActive(FlashPromotion flashPromotion) {
        if (flashPromotion.getId()!=null&&!"".equals(flashPromotion)){
           return flashPromotionMapper.updateById(flashPromotion);
        }
        return 0;
    }


}
